<?php
if (!defined('BASEPATH'))
	exit('No direct script access allowed');

/**
* 
*/
class dashboard extends CI_Controller
{
	
	function __construct()
	{
		parent::__construct();
		$this->load->helper(array('url', 'form'));
		$this->load->model(array('m_login', 'm_barang'));
	}

  function index() {
   $cek = $this->session->userdata('logged_in');
   if (empty($cek)) {
    header('location:' . site_url('login'));
  } else {
    $conf['ctrl']['navigation1'] = 'active';
    $conf['ctrl']['in'] = '';
    $conf['ctrl']['navigation2'] = $conf['ctrl']['navigation3'] = $conf['ctrl']['navigation4'] ='';
    $data['total'] = $this->db->count_all('tb_siswa');
    $this->db->where('keterangan', '1');
    $data['masuk'] = $this->db->count_all_results('tb_siswa');
    $this->db->where('keterangan', '0');
    $data['keluar'] = $this->db->count_all_results('tb_siswa');
    $this->load->view('global/header', $conf);
    $this->load->view('content/dashboard', $data);
    $this->load->view('global/footer');

  }
}

function listbarang(){
 $cek = $this->session->userdata('logged_in');
 if (empty($cek)) {
  header('location:' . site_url('login'));
} else {
  $conf['ctrl']['navigation2'] = 'active';
  $conf['ctrl']['in'] = 'in';
  $conf['ctrl']['navigation1'] = $conf['ctrl']['navigation3'] = $conf['ctrl']['navigation4'] ='';
  $data['data'] = $this->m_barang->ambil();
  $this->load->view('global/header', $conf);
  $this->load->view('content/listbarang', $data);
  $this->load->view('global/footer');

}
}


function tambahdata(){
 $cek = $this->session->userdata('logged_in');
 if (empty($cek)) {
  header('location:' . site_url('login'));
} else {
 $conf['ctrl']['navigation3'] = 'active';
 $conf['ctrl']['in'] = 'in';
 $conf['ctrl']['navigation2'] = $conf['ctrl']['navigation1'] = $conf['ctrl']['navigation4'] ='';
 $data['ctrl']['jenis'] = 'Tambah';
 $data['ctrl']['id'] = '';
 $data['nomor_ak'] = $data['nama'] = $data['pangkat'] = $data['kodesenjata'] = $data['nosenjata'] = $data['foto'] ="";
 $this->load->view('global/header', $conf);
 $this->load->view('content/tambahdata', $data);
 $this->load->view('global/footer');

}
}

function editdata($id =''){
  $cek = $this->session->userdata('logged_in');
  if (empty($cek)) {
    header('location:' . site_url('login'));
  } else {
   $conf['ctrl']['navigation3'] = '';
   $conf['ctrl']['in'] = 'in';
   $conf['ctrl']['navigation2'] = $conf['ctrl']['navigation1'] = $conf['ctrl']['navigation4'] ='';
   $data['ctrl']['jenis'] = 'Edit';
   $data['ctrl']['id'] = $id;
   $ambil = $this->m_barang->ambilbyid($id)->row();
   $data['nomor_ak'] = $ambil->nomor_ak;
   $data['nama'] = $ambil->nama;
   $data['pangkat'] = $ambil->pangkat;
   $data['nosenjata'] = $ambil->nomor_senjata;
   $data['foto'] = $ambil->foto;
   $this->load->view('global/header', $conf);
   $this->load->view('content/tambahdata', $data);
   $this->load->view('global/footer');

 }
}
function hapusdata($id='') {
  $cek = $this->session->userdata('logged_in');
  if (empty($cek)) {
    header('location:' . site_url('admin'));
  } else {
    $this->m_barang->hapus($id);
    redirect('dashboard/listbarang');
  }
}

function barcode($id=''){
 $cek = $this->session->userdata('logged_in');
 if (empty($cek)) {
  header('location:' . site_url('login'));
} else {
  $this->load->library('ciqrcode');
  $conf['ctrl']['navigation3'] = '';
  $conf['ctrl']['in'] = 'in';
  $conf['ctrl']['navigation2'] = $conf['ctrl']['navigation1'] = $conf['ctrl']['navigation4']='';
  $data = $this->m_barang->ambilbyid($id)->row();
  $params['data'] = $data->nama.';'.$data->pangkat.';'.$data->nomor_ak.';'.$data->nomor_senjata;
  header("Content-Type: image/png");
 //$params['data'] = 'This is a text to encode become QR Code';
  //print_r($params);
  $this->ciqrcode->generate($params);
}
}

function simpandata()
{
  $cek = $this->session->userdata('logged_in');
  if (empty($cek)) {
    header('location:' . site_url('login'));
  } else {
    $this->form_validation->set_rules('nomor_ak', 'No AK', 'trim|required');
    $this->form_validation->set_rules('nama', 'Nama', 'trim|required');
    $this->form_validation->set_rules('pangkat', 'Pangkat', 'trim|required');
    $this->form_validation->set_rules('nosenjata', 'No Senjata', 'trim|required');
    if ($this->input->post('submit')) {
      if ($this->form_validation->run() == FALSE) {
       $conf['ctrl']['navigation3'] = 'active';
       $conf['ctrl']['in'] = 'in';
       $conf['ctrl']['navigation2'] = $conf['ctrl']['navigation4'] = $conf['ctrl']['navigation1'] ='';
       $data['ctrl']['jenis'] = $this->input->post('st');
       $data['nomor_ak'] = $this->input->post('nomor_ak');
       $data['nama'] = $this->input->post('nama');
       $data['pangkat'] = $this->input->post('pangkat');
       $data['nosenjata'] = $this->input->post('nosenjata');
       $this->load->view('global/header', $conf);
       $this->load->view('content/tambahdata', $data);
       $this->load->view('global/footer');
     }else {
      $foto = $this->input->post('foto');
      $st = $this->input->post('st');
      if($foto == ""){
       $this->do_upload();
      }else{
        if ($st == "Tambah") {
          $this->m_barang->tambah();
        } else if ($st == "Edit") {
          $this->m_barang->update($foto);
        }
      }
        redirect('dashboard/tambahdata');
      }
    }
  }
}

function listpengguna(){
 $cek = $this->session->userdata('logged_in');
 if (empty($cek)) {
  header('location:' . site_url('login'));
} else {
  $conf['ctrl']['navigation4'] = 'active';
  $conf['ctrl']['in'] = 'in';
  $conf['ctrl']['navigation1'] = $conf['ctrl']['navigation2'] =$conf['ctrl']['navigation3'] ='';
  $data['data'] = $this->m_login->ambil();
  $this->load->view('global/header', $conf);
  $this->load->view('content/listpengguna', $data);
  $this->load->view('global/footer');

}
}

function hapuspengguna($id='') {
  $cek = $this->session->userdata('logged_in');
  if (empty($cek)) {
    header('location:' . site_url('admin'));
  } else {
    $this->m_login->hapus($id);
    redirect('dashboard/listpengguna');
  }
}

function tambahpengguna(){
 $cek = $this->session->userdata('logged_in');
 if (empty($cek)) {
  header('location:' . site_url('login'));
} else {
 $conf['ctrl']['navigation4'] = 'active';
 $conf['ctrl']['in'] = 'in';
 $conf['ctrl']['navigation2'] = $conf['ctrl']['navigation1'] = $conf['ctrl']['navigation3'] ='';
 $data['ctrl']['jenis'] = 'Tambah';
 $data['ctrl']['id'] = '';
 $data['username'] = $data['password'] ="";
 $this->load->view('global/header', $conf);
 $this->load->view('content/tambahpengguna', $data);
 $this->load->view('global/footer');

}
}

function simpanpengguna()
{
  $cek = $this->session->userdata('logged_in');
  if (empty($cek)) {
    header('location:' . site_url('login'));
  } else {
    $this->form_validation->set_rules('username', 'username', 'trim|required');
    $this->form_validation->set_rules('password', 'password', 'trim|required');
    if ($this->input->post('submit-user')) {
      if ($this->form_validation->run() == FALSE) {
       $conf['ctrl']['navigation4'] = 'active';
       $conf['ctrl']['in'] = 'in';
       $conf['ctrl']['navigation2'] = $conf['ctrl']['navigation3'] = $conf['ctrl']['navigation1'] ='';
       $data['username'] = $this->input->post('username');
       $data['password]'] = $this->input->post('password');
       $this->load->view('global/header', $conf);
       $this->load->view('content/tambahpengguna', $data);
       $this->load->view('global/footer');
     }else {
       $this->m_login->tambah();
       redirect('dashboard/tambahpengguna');
     } 
   }
 }
}

function hapus_foto($id=""){
  $cek = $this->session->userdata('logged_in');
  if (empty($cek)) {
    header('location:' . site_url('admin'));
  } else {
    $data = $this->m_barang->ambilbyid($id)->row();
    $foto = $data->foto;
    unlink('./foto/'.$foto);
    $this->m_barang->hapus_foto($id);
    redirect('dashboard/editdata/'.$id);
  }
}

function do_upload($userfile='') {
  $config['upload_path'] = './foto/';
  $config['allowed_types'] = 'gif|jpg|png|jpeg';
  $config['max_size'] = '1000';
  $config['max_width'] = '1024';
  $config['max_height'] = '768';

  $this -> load -> library('upload', $config);
    // echo "<pre>";
    // print_r($this->upload->data());
    // echo "</pre>";
  if (!$this -> upload -> do_upload('userfile')) {
    $error = $this -> upload -> display_errors();
    print_r($error);
  } else {
    $data = array('upload_data' => $this -> upload -> data());
      //print_r($link);
      //print_r($data);

    foreach ($data as $key => $value) {
        // print_r($value['file_name']);
      $fail = $value['file_name'];
    }
    $st = $this->input->post('st');
    if ($st == "Tambah") {
      $this->m_barang->tambah($fail);
    } else if ($st == "Edit") {
      $this->m_barang->update($fail);
    }
      // print_r('faak : '.$fail);
      //$this->load->view('upload_success', $data);
  }
}
}
?>